
//package com.key.controller.admin.assetManagement;
//
//import com.key.common.R;
//import com.key.common.dto.AssetsAddDTO;
//import com.key.common.dto.AssetsFindDTO;
//import com.key.common.vo.AssetsVO;
//import com.key.entity.Assets;
//import com.key.service.AssetsService;
//import com.key.utils.EntityConversion;
//import io.swagger.annotations.*;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.*;
//
//import java.math.BigDecimal;
//import java.sql.Timestamp;
//import java.util.*;
//
///**
// * @author wei
// * @create 2023-02-17 10:34
// */
//@Controller
//@Api(value = "资产处理",tags = "资产处理")
//@RequestMapping("/assets")
//public class AssetsController {
//
//    @Autowired
//    EntityConversion entityConversion;
//    @Autowired
//    AssetsService assetsService;
//
//    /**
//     * 分页查询资产表，含高级搜索
//     * @author wei
//     * @param assetsFindDTO
//     * @return
//     */
//    @RequestMapping(value = "/findAssetsList", method = RequestMethod.POST)
//    @ApiOperation(value = "分页查询资产表",notes = "分页查询资产表，含高级搜索",httpMethod = "post", response = R.class)
//    @ResponseBody
//    public Map findAssetsList(@RequestBody @ApiParam(name = "AssetsFindDTO",value = "查询资产时需要的参数",required = true) AssetsFindDTO assetsFindDTO){
//        Map<String, Object> respMap = new HashMap<>();
//
//        try{
//            List<AssetsVO> assetsVOList = assetsService.findAssetsList(assetsFindDTO);
//            respMap.put("code", 200);
//            respMap.put("count", assetsService.getAssetsCount(assetsFindDTO));
//            respMap.put("msg", "查询成功");
//            respMap.put("data", assetsVOList);
//            BigDecimal sum = new BigDecimal("0");
//            for(AssetsVO assetsVO : assetsVOList){
//                sum = sum.add(assetsVO.getAssetsMoney());
//            }
//            respMap.put("moneySum", sum);
//        }catch (Exception e){
//            respMap.put("code", 500);
//            respMap.put("msg", "后台代码异常");
//        }
//
//        return respMap;
//    }
//
////    /**
////     * 根据id查询资产
////     * @author wei
////     * @param assetsIds
////     * @return
////     */
////    @RequestMapping(value = "/findAssetsById", method = RequestMethod.GET)
////    @ResponseBody
////    @ApiOperation(value = "根据id查询资产",notes = "根据资产id查询资产，可查多个",httpMethod = "get", response = R.class)
////    @ApiImplicitParam(name = "assetsIds",value = "资产id数组",required = true,dataType = "long",allowMultiple=true,paramType = "query")
////    public Map findAssetsById(@RequestParam(name = "assetsIds") List<Long> assetsIds){
////        Map<String, Object> respMap = new HashMap<>();
////        //TODO
////        List<AssetsVO> assetsVOList = new ArrayList<>();
////
////
////        respMap.put("code", 200);
////        respMap.put("count", 1);
////        respMap.put("msg", "success");
////        respMap.put("data", assetsVOList);
////        respMap.put("moneySum", 100000);
////
////        return respMap;
////    }
//
//    /**
//     * 根据id删除资产，可批量删
//     * @author wei
//     * @param assetsIds
//     * @return
//     */
//    @RequestMapping(value = "/removeAssets", method = RequestMethod.POST)
//    @ResponseBody
//    @ApiOperation(value = "根据id删除资产",notes = "根据资产id删除资产，可批量删除",httpMethod = "post", response = R.class)
//    @ApiImplicitParam(name = "assetsIds",value = "资产id数组",required = true,dataType = "long",allowMultiple=true,paramType = "query")
//    public R removeAssets(@RequestParam(name = "assetsIds") List<Long> assetsIds){
//        R r;
//        boolean result = assetsService.removeAssets(assetsIds);
//        if(result){
//            r = new R(200,"删除成功");
//        }else {
//            r = new R(500,"删除失败");
//        }
//        return r;
//    }
//
//    /**
//     * 修改资产，不可批量改
//     * @author wei
//     * @param assetsAddDTO
//     * @return
//     */
//    @RequestMapping(value = "/editAssets", method = RequestMethod.POST)
//    @ResponseBody
//    @ApiOperation(value = "修改资产",notes = "获取前端页面的信息以完成修改",httpMethod = "post", response = R.class)
//    public R editAssets(@RequestBody @ApiParam(name = "AssetsAddDTO",value = "修改资产需要填入的属性",required = true)AssetsAddDTO assetsAddDTO){
//        R r;
//        Assets assets = entityConversion.AssetsAddDTOToAssets(assetsAddDTO);
//        boolean result = assetsService.editAssets(assets);
//        if(result){
//            r = new R(200,"修改成功");
//        }else {
//            r = new R(500,"修改失败");
//        }
//        return r;
//    }
//
//    /**
//     * 新建资产
//     * @author wei
//     * @param assetsAddDTO
//     * @return
//     */
//    @RequestMapping(value = "/addAssets", method = RequestMethod.POST)
//    @ResponseBody
//    @ApiOperation(value = "新建资产",notes = "获取前端页面的信息以完成新建",httpMethod = "post", response = R.class)
//    public R addAssets(@RequestBody @ApiParam(name = "AssetsAddDTO",value = "新建资产需要填入的属性",required = true)AssetsAddDTO assetsAddDTO){
//        R r;
//        Date date = new Date();
//        Timestamp now = new Timestamp(date.getTime());
//        Assets assets = entityConversion.AssetsAddDTOToAssets(assetsAddDTO,now);
//
//        System.out.println(assetsAddDTO);
//        System.out.println(assets);
//
//        boolean result = assetsService.addAssets(assets);
//        if(result){
//            r = new R(200,"新建成功");
//        }else {
//            r = new R(500,"新建失败");
//        }
//        return r;
//    }
//
//    @RequestMapping(value = "/copyAssets", method = RequestMethod.POST)
//    @ResponseBody
//    @ApiOperation(value = "根据id复制资产",notes = "根据资产id复制资产，不可批量复制",httpMethod = "post", response = R.class)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "assetsId",value = "资产id",required = true,dataType = "long",paramType = "query"),
//            @ApiImplicitParam(name = "number",value = "复制个数",required = true,dataType = "int",paramType = "query")
//    })
//    public R copyAssets(@RequestParam(name = "assetsId") Long assetsId,
//                        @RequestParam(name = "number") Integer number){
//        R r;
//        boolean result = assetsService.copyAssets(assetsId, number);
//        if(result){
//            r = new R(200,"复制成功");
//        }else {
//            r = new R(500,"复制失败");
//        }
//        return r;
//    }
//}

package com.key.controller.admin.assetManagement;

import com.key.common.R;
import com.key.common.dto.AssetsAddDTO;
import com.key.common.dto.AssetsFindDTO;
import com.key.common.vo.AssetsVO;
import com.key.entity.Assets;
import com.key.service.AssetCodeService;
import com.key.service.AssetsService;
import com.key.utils.EntityConversion;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;

/**
 * @author wei
 * @create 2023-02-17 10:34
 */
@Controller
@Api(value = "资产处理",tags = "资产处理")
@RequestMapping("/assets")
public class AssetsController {

    @Autowired
    private AssetCodeService assetCodeService;
    @Autowired
    private EntityConversion entityConversion;
    @Autowired
    private AssetsService assetsService;

    /**
     * 分页查询资产表，含高级搜索
     * @author wei
     * @param assetsFindDTO
     * @return
     */
    @RequestMapping(value = "/findAssetsList", method = RequestMethod.POST)
    @ApiOperation(value = "分页查询资产表",notes = "分页查询资产表，含高级搜索",httpMethod = "post", response = R.class)
    @ResponseBody
    public Map findAssetsList(@RequestBody @ApiParam(name = "AssetsFindDTO",value = "查询资产时需要的参数",required = true) AssetsFindDTO assetsFindDTO){
        Map<String, Object> respMap = new HashMap<>();

        try{
            Map<String, Object> map = assetsService.findAssetsList(assetsFindDTO);
            List<AssetsVO> assetsVOList = (List<AssetsVO>) map.get("assetsVOList");
            Integer count = (Integer) map.get("count");

            respMap.put("code", 200);
            respMap.put("count", count);
            respMap.put("msg", "查询成功");
            respMap.put("data", assetsVOList);
            BigDecimal sum = new BigDecimal("0");
            for(AssetsVO assetsVO : assetsVOList){
                sum = sum.add(assetsVO.getAssetsMoney());
            }
            respMap.put("moneySum", sum);
        }catch (Exception e){
            respMap.put("code", 500);
            respMap.put("msg", "后台代码异常");
        }

        return respMap;
    }

//    /**
//     * 根据id查询资产
//     * @author wei
//     * @param assetsIds
//     * @return
//     */
//    @RequestMapping(value = "/findAssetsById", method = RequestMethod.GET)
//    @ResponseBody
//    @ApiOperation(value = "根据id查询资产",notes = "根据资产id查询资产，可查多个",httpMethod = "get", response = R.class)
//    public Map findAssetsById(@RequestBody @ApiParam(name = "assetsIds",value = "资产id数组",required = true) List<Long> assetsIds){
//        System.out.println(assetsIds);
//
//        Map<String, Object> respMap = new HashMap<>();
//        //TODO
//        List<AssetsVO> assetsVOList = new ArrayList<>();
//
//
//        respMap.put("code", 200);
//        respMap.put("count", 1);
//        respMap.put("msg", "success");
//        respMap.put("data", assetsVOList);
//        respMap.put("moneySum", 100000);
//
//        return respMap;
//    }

    /**
     * 根据id删除资产，可批量删
     * @author wei
     * @param assetsIds
     * @return
     */
    @RequestMapping(value = "/removeAssets", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据id删除资产",notes = "根据资产id删除资产，可批量删除",httpMethod = "post", response = R.class)
    public R removeAssets(@RequestBody @ApiParam(name = "assetsIds",value = "资产id数组",required = true) List<Long> assetsIds){
        R r;
        boolean result = assetsService.removeAssets(assetsIds);
        if(result){
            r = new R(200,"删除成功");
        }else {
            r = new R(500,"删除失败");
        }
        return r;
    }

    /**
     * 修改资产，不可批量改
     * @author wei
     * @param assetsAddDTO
     * @return
     */
    @RequestMapping(value = "/editAssets", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改资产",notes = "获取前端页面的信息以完成修改",httpMethod = "post", response = R.class)
    public R editAssets(@RequestBody @ApiParam(name = "AssetsAddDTO",value = "修改资产需要填入的属性",required = true)AssetsAddDTO assetsAddDTO){
        R r;
        Assets assets = entityConversion.AssetsAddDTOToAssets(assetsAddDTO);
        boolean result = assetsService.editAssets(assets);
        if(result){
            r = new R(200,"修改成功");
        }else {
            r = new R(500,"修改失败");
        }
        return r;
    }

    /**
     * 新建资产
     * @author wei
     * @param assetsAddDTO
     * @return
     */
    @RequestMapping(value = "/addAssets", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新建资产",notes = "获取前端页面的信息以完成新建",httpMethod = "post", response = R.class)
    public R addAssets(@RequestBody @ApiParam(name = "AssetsAddDTO",value = "新建资产需要填入的属性",required = true)AssetsAddDTO assetsAddDTO){
        R r;
        Date date = new Date();
        Timestamp now = new Timestamp(date.getTime());
        Assets assets = entityConversion.AssetsAddDTOToAssets(assetsAddDTO,now);

        assets.setAssetsCode(assetCodeService.getCode());

        boolean result = assetsService.addAssets(assets);
        if(result){
            r = new R(200,"新建成功");
        }else {
            r = new R(500,"新建失败");
        }
        return r;
    }

    @RequestMapping(value = "/copyAssets", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据id复制资产",notes = "根据资产id复制资产，不可批量复制",httpMethod = "post", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "assetsId",value = "资产id",required = true,dataType = "long",paramType = "query"),
            @ApiImplicitParam(name = "number",value = "复制个数",required = true,dataType = "int",paramType = "query")
    })
    public R copyAssets(@RequestParam(name = "assetsId") Long assetsId,
                        @RequestParam(name = "number") Integer number){
        R r;
        boolean result = assetsService.copyAssets(assetsId, number);
        if(result){
            r = new R(200,"复制成功");
        }else {
            r = new R(500,"复制失败");
        }
        return r;
    }
}

